home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Fortran to C conversion help?
- Date: 4 Mar 1996 19:15:03 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4hgbjnINN136@keats.ugrad.cs.ubc.ca>
- References: <4hf04k$ogs@pipe3.nyc.pipeline.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4hf04k$ogs@pipe3.nyc.pipeline.com>,
- Doug Kolmar <dkolmar@grovestocktn.com> wrote:
- >
- >Hello folks,
- >
- >I'm sort of a weekend C programmer so perhaps I've missed something, but
- >I'm trying to convert a function in FORTRAN to C.
- >
- >Attached is the FORTRAN code followed by the C code that I've written. The
- >C program will compile and run, but does not yield the correct output. I.E.
- >integers in the range 0 to 31.
- >
- >Except for the differences in the range of the RAND functions, I know
- >nothing about FORTRAN, so am I missing something implicit in the code? Any
- >help would be greatly appreciated.
-
- Use an automatic translator. Here is the output of f2c:
-
- First of all, I had to fix up your fortran program. Fortran requires that the
- first six characters of each punched card^H^H^H^H^H^H^H^H^H^H^H^Hsource line
- be blank, or contain a line number or 'C' character for comment. Secondly, you
- are missing a line 20, the target of the GO TO statement. I just stuck in a
- line 20 wherever I liked:
-
- file fortran.f:
-
-
- FUNCTION I1OVRF(LAST)
- C THE ARGUMENT, LAST, IS THE PREVIOUS VALUE OF THE SEQUENCE
- NEW=0
- C THE VARIABLE K EQUALS ONE-HALF THE NUMBER OF POSSIBLE VALUES
- K=16
- L=LAST
- C THE VARIABLE PROBIT EQUALS 1/(NUMBER OF POSSIBLE VALUES)
- PROBIT = .03125
- 20 J=L/K
- IF (J.EQ.1) L=L-K
- U=RAND(0)
- IF (U.LT.PROBIT) J=1-J
- NEW = NEW+J*K
- K=K/2
- PROBIT=PROBIT*2
- IF (K.GE.1) GO TO 20
- I1OVRF=NEW
- RETURN
- END
-
- file fortran.c produced by AT&T's f2c:
-
-
- /* fortran.f -- translated by f2c (version 19940305).
- You must link the resulting object file with the libraries:
- -lf2c -lm (in that order)
- */
-
- #include "f2c.h"
-
- /* Table of constant values */
-
- static integer c__0 = 0;
-
- integer i1ovrf_(last)
- integer *last;
- {
- /* System generated locals */
- integer ret_val;
-
- /* Local variables */
- extern doublereal rand_();
- static integer j, k, l;
- static real u, probit;
- static integer new_;
-
- /* THE ARGUMENT, LAST, IS THE PREVIOUS VALUE OF THE SEQUENCE */
- new_ = 0;
- /* THE VARIABLE K EQUALS ONE-HALF THE NUMBER OF POSSIBLE VALUES */
- k = 16;
- l = *last;
- /* THE VARIABLE PROBIT EQUALS 1/(NUMBER OF POSSIBLE VALUES) */
- probit = (float).03125;
- L20:
- j = l / k;
- if (j == 1) {
- l -= k;
- }
- u = rand_(&c__0);
- if (u < probit) {
- j = 1 - j;
- }
- new_ += j * k;
- k /= 2;
- probit *= 2;
- if (k >= 1) {
- goto L20;
- }
- ret_val = new_;
- return ret_val;
- } /* i1ovrf_ */
-
-
- This is one of those rare occurances when the output of a translator is more
- readable than the source.
- --
-
-